{
  "cells": [
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "## 2.11 \u5220\u9664\u5b57\u7b26\u4e32\u4e2d\u4e0d\u9700\u8981\u7684\u5b57\u7b26\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "### \u95ee\u9898\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u4f60\u60f3\u53bb\u6389\u6587\u672c\u5b57\u7b26\u4e32\u5f00\u5934\uff0c\u7ed3\u5c3e\u6216\u8005\u4e2d\u95f4\u4e0d\u60f3\u8981\u7684\u5b57\u7b26\uff0c\u6bd4\u5982\u7a7a\u767d\u3002"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "### \u89e3\u51b3\u65b9\u6848\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "strip() \u65b9\u6cd5\u80fd\u7528\u4e8e\u5220\u9664\u5f00\u59cb\u6216\u7ed3\u5c3e\u7684\u5b57\u7b26\u3002 lstrip() \u548c rstrip() \u5206\u522b\u4ece\u5de6\u548c\u4ece\u53f3\u6267\u884c\u5220\u9664\u64cd\u4f5c\u3002\n\u9ed8\u8ba4\u60c5\u51b5\u4e0b\uff0c\u8fd9\u4e9b\u65b9\u6cd5\u4f1a\u53bb\u9664\u7a7a\u767d\u5b57\u7b26\uff0c\u4f46\u662f\u4f60\u4e5f\u53ef\u4ee5\u6307\u5b9a\u5176\u4ed6\u5b57\u7b26\u3002\u6bd4\u5982\uff1a"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "# Whitespace stripping\ns = ' hello world \\n'\ns.strip()"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "s.lstrip()"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "s.rstrip()"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "# Character stripping\nt = '-----hello====='\nt.lstrip('-')"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "t.strip('-=')"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "### \u8ba8\u8bba\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u8fd9\u4e9b strip() \u65b9\u6cd5\u5728\u8bfb\u53d6\u548c\u6e05\u7406\u6570\u636e\u4ee5\u5907\u540e\u7eed\u5904\u7406\u7684\u65f6\u5019\u662f\u7ecf\u5e38\u4f1a\u88ab\u7528\u5230\u7684\u3002\n\u6bd4\u5982\uff0c\u4f60\u53ef\u4ee5\u7528\u5b83\u4eec\u6765\u53bb\u6389\u7a7a\u683c\uff0c\u5f15\u53f7\u548c\u5b8c\u6210\u5176\u4ed6\u4efb\u52a1\u3002"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u4f46\u662f\u9700\u8981\u6ce8\u610f\u7684\u662f\u53bb\u9664\u64cd\u4f5c\u4e0d\u4f1a\u5bf9\u5b57\u7b26\u4e32\u7684\u4e2d\u95f4\u7684\u6587\u672c\u4ea7\u751f\u4efb\u4f55\u5f71\u54cd\u3002\u6bd4\u5982\uff1a"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "s = ' hello     world \\n'\ns = s.strip()\ns"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u5982\u679c\u4f60\u60f3\u5904\u7406\u4e2d\u95f4\u7684\u7a7a\u683c\uff0c\u90a3\u4e48\u4f60\u9700\u8981\u6c42\u52a9\u5176\u4ed6\u6280\u672f\u3002\u6bd4\u5982\u4f7f\u7528 replace() \u65b9\u6cd5\u6216\u8005\u662f\u7528\u6b63\u5219\u8868\u8fbe\u5f0f\u66ff\u6362\u3002\u793a\u4f8b\u5982\u4e0b\uff1a"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "s.replace(' ', '')"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "import re\nre.sub('\\s+', ' ', s)"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u901a\u5e38\u60c5\u51b5\u4e0b\u4f60\u60f3\u5c06\u5b57\u7b26\u4e32 strip \u64cd\u4f5c\u548c\u5176\u4ed6\u8fed\u4ee3\u64cd\u4f5c\u76f8\u7ed3\u5408\uff0c\u6bd4\u5982\u4ece\u6587\u4ef6\u4e2d\u8bfb\u53d6\u591a\u884c\u6570\u636e\u3002\n\u5982\u679c\u662f\u8fd9\u6837\u7684\u8bdd\uff0c\u90a3\u4e48\u751f\u6210\u5668\u8868\u8fbe\u5f0f\u5c31\u53ef\u4ee5\u5927\u663e\u8eab\u624b\u4e86\u3002\u6bd4\u5982\uff1a"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "with open(filename) as f:\n    lines = (line.strip() for line in f)\n    for line in lines:\n        print(line)"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u5728\u8fd9\u91cc\uff0c\u8868\u8fbe\u5f0f lines = (line.strip() for line in f) \u6267\u884c\u6570\u636e\u8f6c\u6362\u64cd\u4f5c\u3002\n\u8fd9\u79cd\u65b9\u5f0f\u975e\u5e38\u9ad8\u6548\uff0c\u56e0\u4e3a\u5b83\u4e0d\u9700\u8981\u9884\u5148\u8bfb\u53d6\u6240\u6709\u6570\u636e\u653e\u5230\u4e00\u4e2a\u4e34\u65f6\u7684\u5217\u8868\u4e2d\u53bb\u3002\n\u5b83\u4ec5\u4ec5\u53ea\u662f\u521b\u5efa\u4e00\u4e2a\u751f\u6210\u5668\uff0c\u5e76\u4e14\u6bcf\u6b21\u8fd4\u56de\u884c\u4e4b\u524d\u4f1a\u5148\u6267\u884c strip \u64cd\u4f5c\u3002"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u5bf9\u4e8e\u66f4\u9ad8\u9636\u7684strip\uff0c\u4f60\u53ef\u80fd\u9700\u8981\u4f7f\u7528 translate() \u65b9\u6cd5\u3002\u8bf7\u53c2\u9605\u4e0b\u4e00\u8282\u4e86\u89e3\u66f4\u591a\u5173\u4e8e\u5b57\u7b26\u4e32\u6e05\u7406\u7684\u5185\u5bb9\u3002"
      ]
    }
  ],
  "metadata": {
    "kernelspec": {
      "display_name": "Python 3",
      "language": "python",
      "name": "python3"
    },
    "language_info": {
      "codemirror_mode": {
        "name": "ipython",
        "version": 3
      },
      "file_extension": ".py",
      "mimetype": "text/x-python",
      "name": "python",
      "nbconvert_exporter": "python",
      "pygments_lexer": "ipython3",
      "version": "3.7.1"
    },
    "toc": {
      "base_numbering": 1,
      "nav_menu": {},
      "number_sections": true,
      "sideBar": true,
      "skip_h1_title": true,
      "title_cell": "Table of Contents",
      "title_sidebar": "Contents",
      "toc_cell": false,
      "toc_position": {},
      "toc_section_display": true,
      "toc_window_display": true
    }
  },
  "nbformat": 4,
  "nbformat_minor": 2
}